import {Base} from '../../utils/base.js';
var base=new Base();
// pages/authorize/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    canIUseGetUserProfile: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
    // 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.showLoading({ title: '请稍后...' })
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        base.request({
          url: '/set_user_info',
          type: "POST",
          data: res,
          success: function (res) {
            console.log(res)
            if(res.data.code){
              wx.hideLoading()
              wx.reLaunch({
                url:'/pages/my/index' 
              })
            }else{
              if(res.statusCode!=401){
                wx.showToast({
                  title: '',
                  icon: 'none'
                })
                setTimeout(function () {
                  wx.reLaunch({
                    url: '/pages/index/index'
                  })
                }, 1500)
              }
            }
          },
          fail: function (res) {
            wx.switchTab({
              url: '/pages/index/index'
            })
          }
        });
      }
    })
  },
  onGotUserInfo:function(e){
    var msg=e.detail.errMsg;
    if (msg =="getUserInfo:ok"){
      wx.showLoading({ title: '请稍后...' })
      this._setting((res)=>{
        if(res.data.code){
          wx.hideLoading()
          wx.reLaunch({
            url:'/pages/my/index' 
          })
        }else{
          if(res.statusCode!=401){
            wx.showToast({
              title: '',
              icon: 'none'
            })
            setTimeout(function () {
              wx.reLaunch({
                url: '/pages/index/index'
              })
            }, 1500)
          }
        }
      })
    }else{
      wx.switchTab({
        url: '/pages/index/index'
      })
    }
  }, 

// 下方方法 已废弃
  onGotUserInfo:function(e){
    var msg=e.detail.errMsg;
    if (msg =="getUserInfo:ok"){
      wx.showLoading({ title: '请稍后...' })
      this._setting((res)=>{
        if(res.data.code){
          wx.hideLoading()
          wx.reLaunch({
            url:'/pages/my/index' 
          })
        }else{
          if(res.statusCode!=401){
            wx.showToast({
              title: '',
              icon: 'none'
            })
            setTimeout(function () {
              wx.reLaunch({
                url: '/pages/index/index'
              })
            }, 1500)
          }
        }
      })
    }else{
      wx.switchTab({
        url: '/pages/index/index'
      })
    }
  }, 

  // 设置用户信息
  _setting:function(callback) {
    var that = this;
    wx.getSetting({
      success: (res) => {
        console.log(res)
        if (res.authSetting['scope.userInfo']) {
          that._setUserInfo((result) => {
            callback && callback(result)
          }) 
        }
      }
    })
  },
  
  // 将用户信息传入数据库
  _setUserInfo:function(callback) {
    wx.getUserInfo({
      lang: 'zh_CN',
      success: function (res) {
        console.log(res)
        base.request({
          url: '/set_user_info',
          type: "POST",
          data: res,
          success: function (res) {
            console.log(res)
            callback && callback(res);
          },
          fail: function (res) {
            callback && callback(res)
          }
        });
      }
    })
  }
})